package com.ktjiaoyu.crm.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//看不懂了。。。断点都不会进   这是因为你要运行main页面才会进这里面
public class AuthorizationFilter implements Filter {

    @Override
    public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest=(HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse=(HttpServletResponse) servletResponse;
        if(httpRequest.getSession().getAttribute("loginUser")==null){
            httpResponse.setContentType("text/html;charset=UTF-8");
            PrintWriter out=httpResponse.getWriter();
            out.print("<script>alert('请先进行登陆，再进行后续操作！(Filter控制)');location.href='"+httpRequest.getContextPath()+
                    "/login.jsp';</script>");
        }else{
            filterChain.doFilter(httpRequest,httpResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
